home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / wrlib / xpm.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-14  |  7.0 KB  |  279 lines

  1. /* xpm.c - load XPM image from file
  2.  * 
  3.  *  Raster graphics library
  4.  * 
  5.  *  Copyright (c) 1997 Alfredo K. Kojima
  6.  *
  7.  *  This library is free software; you can redistribute it and/or
  8.  *  modify it under the terms of the GNU Library General Public
  9.  *  License as published by the Free Software Foundation; either
  10.  *  version 2 of the License, or (at your option) any later version.
  11.  *  
  12.  *  This library is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  *  Library General Public License for more details.
  16.  *  
  17.  *  You should have received a copy of the GNU Library General Public
  18.  *  License along with this library; if not, write to the Free
  19.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  */
  21.  
  22. #include <config.h>
  23.  
  24.  
  25. #ifdef USE_XPM
  26.  
  27. #include <X11/Xlib.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <X11/xpm.h>
  32.  
  33. #include "wraster.h"
  34.  
  35. RImage*
  36. RGetImageFromXPMData(RContext *context, char **xpmData)
  37. {
  38.     Display *dpy = context->dpy;
  39.     Colormap cmap = context->cmap;
  40.     RImage *image;
  41.     XpmImage xpm;
  42.     unsigned char *color_table[4];
  43.     unsigned char *data;
  44.     int *p;
  45.     int i;
  46.     
  47.     i = XpmCreateXpmImageFromData(xpmData, &xpm, (XpmInfo *)NULL);
  48.     if (i!=XpmSuccess) {
  49.     switch (i) {
  50.      case XpmOpenFailed:
  51.         RErrorCode = RERR_OPEN;
  52.         break;
  53.      case XpmFileInvalid:
  54.         RErrorCode = RERR_BADIMAGEFILE;
  55.         break;
  56.      case XpmNoMemory:
  57.         RErrorCode = RERR_NOMEMORY;
  58.         break;
  59.      default:
  60.         RErrorCode = RERR_BADIMAGEFILE;
  61.         break;
  62.     }
  63.     return NULL;
  64.     }
  65.     if (xpm.height<1 || xpm.width < 1) {
  66.     RErrorCode = RERR_BADIMAGEFILE;
  67.     XpmFreeXpmImage(&xpm);
  68.     return NULL;
  69.     }
  70.  
  71.     if (xpm.colorTable==NULL) {
  72.     RErrorCode = RERR_BADIMAGEFILE;
  73.     XpmFreeXpmImage(&xpm);
  74.     return NULL;
  75.     }
  76.     image = RCreateImage(xpm.width, xpm.height, True);
  77.     if (!image) {
  78.     XpmFreeXpmImage(&xpm);
  79.     return NULL;
  80.     }
  81.     
  82.     /* make color table */
  83.     for (i=0; i<4; i++) {
  84.     color_table[i] = malloc(xpm.ncolors*sizeof(char));
  85.     if (!color_table[i]) {
  86.         for (i=i-1;i>=0; i--) {
  87.         if (color_table[i])
  88.           free(color_table[i]);
  89.         }
  90.         RDestroyImage(image);
  91.         RErrorCode = RERR_NOMEMORY;
  92.         XpmFreeXpmImage(&xpm);
  93.         return NULL;
  94.     }
  95.     }
  96.  
  97.     for (i=0; i<xpm.ncolors; i++) {
  98.     XColor xcolor;
  99.         char * color = NULL;
  100.  
  101.         if (xpm.colorTable[i].c_color)
  102.              color = xpm.colorTable[i].c_color;
  103.         else if (xpm.colorTable[i].g_color)
  104.              color = xpm.colorTable[i].g_color;
  105.         else if (xpm.colorTable[i].g4_color)
  106.              color = xpm.colorTable[i].g4_color;
  107.         else if (xpm.colorTable[i].m_color)
  108.              color = xpm.colorTable[i].m_color;
  109.         else if (xpm.colorTable[i].symbolic)
  110.              color = xpm.colorTable[i].symbolic;
  111.  
  112.         if (!color) {
  113.         color_table[0][i] = 0xbe;
  114.         color_table[1][i] = 0xbe;
  115.         color_table[2][i] = 0xbe;
  116.         color_table[3][i] = 0xff;
  117.             continue;
  118.         }
  119.     
  120.     if (strncmp(color,"None",4)==0) {
  121.         color_table[0][i]=0;
  122.         color_table[1][i]=0;
  123.         color_table[2][i]=0;
  124.         color_table[3][i]=0;
  125.         continue;
  126.     }
  127.     if (XParseColor(dpy, cmap, color, &xcolor)) {
  128.         color_table[0][i] = xcolor.red>>8;
  129.         color_table[1][i] = xcolor.green>>8;
  130.         color_table[2][i] = xcolor.blue>>8;
  131.         color_table[3][i] = 0xff;
  132.     } else {
  133.         color_table[0][i] = 0xbe;
  134.         color_table[1][i] = 0xbe;
  135.         color_table[2][i] = 0xbe;
  136.         color_table[3][i] = 0xff;
  137.     }
  138.     }
  139.     /* convert pixmap to RImage */
  140.     p = (int*)xpm.data;
  141.     data = image->data;
  142.     for (i=0; i<xpm.width*xpm.height; i++) {
  143.     *(data++)=color_table[0][*p];
  144.     *(data++)=color_table[1][*p];
  145.     *(data++)=color_table[2][*p];
  146.     *(data++)=color_table[3][*p];
  147.     p++;
  148.     }
  149.     for(i=0; i<4; i++) {
  150.     free(color_table[i]);
  151.     }
  152.     XpmFreeXpmImage(&xpm);
  153.     return image;
  154. }
  155.  
  156.  
  157.  
  158. RImage*
  159. RLoadXPM(RContext *context, char *file, int index)
  160. {
  161.     Display *dpy = context->dpy;
  162.     Colormap cmap = context->cmap;
  163.     RImage *image;
  164.     XpmImage xpm;
  165.     unsigned char *color_table[4];
  166.     unsigned char *data;
  167.     int *p;
  168.     int i;
  169.     
  170.     i = XpmReadFileToXpmImage(file, &xpm, (XpmInfo *)NULL);
  171.     if (i!=XpmSuccess) {
  172.     switch (i) {
  173.      case XpmOpenFailed:
  174.         RErrorCode = RERR_OPEN;
  175.         break;
  176.      case XpmFileInvalid:
  177.         RErrorCode = RERR_BADIMAGEFILE;
  178.         break;
  179.      case XpmNoMemory:
  180.         RErrorCode = RERR_NOMEMORY;
  181.         break;
  182.      default:
  183.         RErrorCode = RERR_BADIMAGEFILE;
  184.         break;
  185.     }
  186.     return NULL;
  187.     }
  188.     if (xpm.height<1 || xpm.width < 1) {
  189.     RErrorCode = RERR_BADIMAGEFILE;
  190.     XpmFreeXpmImage(&xpm);
  191.     return NULL;
  192.     }
  193.  
  194.     if (xpm.colorTable==NULL) {
  195.     RErrorCode = RERR_BADIMAGEFILE;
  196.     XpmFreeXpmImage(&xpm);
  197.     return NULL;
  198.     }
  199.     image = RCreateImage(xpm.width, xpm.height, True);
  200.     if (!image) {
  201.     XpmFreeXpmImage(&xpm);
  202.     return NULL;
  203.     }
  204.     
  205.     /* make color table */
  206.     for (i=0; i<4; i++) {
  207.     color_table[i] = malloc(xpm.ncolors*sizeof(char));
  208.     if (!color_table[i]) {
  209.         for (i=i-1;i>=0; i--) {
  210.         if (color_table[i])
  211.           free(color_table[i]);
  212.         }
  213.         RDestroyImage(image);
  214.         RErrorCode = RERR_NOMEMORY;
  215.         XpmFreeXpmImage(&xpm);
  216.         return NULL;
  217.     }
  218.     }
  219.  
  220.     for (i=0; i<xpm.ncolors; i++) {
  221.     XColor xcolor;
  222.         char * color = NULL;
  223.  
  224.         if (xpm.colorTable[i].c_color)
  225.              color = xpm.colorTable[i].c_color;
  226.         else if (xpm.colorTable[i].g_color)
  227.              color = xpm.colorTable[i].g_color;
  228.         else if (xpm.colorTable[i].g4_color)
  229.              color = xpm.colorTable[i].g4_color;
  230.         else if (xpm.colorTable[i].m_color)
  231.              color = xpm.colorTable[i].m_color;
  232.         else if (xpm.colorTable[i].symbolic)
  233.              color = xpm.colorTable[i].symbolic;
  234.  
  235.         if (!color) {
  236.         color_table[0][i] = 0xbe;
  237.         color_table[1][i] = 0xbe;
  238.         color_table[2][i] = 0xbe;
  239.         color_table[3][i] = 0xff;
  240.             continue;
  241.         }
  242.     
  243.     if (strncmp(color,"None",4)==0) {
  244.         color_table[0][i]=0;
  245.         color_table[1][i]=0;
  246.         color_table[2][i]=0;
  247.         color_table[3][i]=0;
  248.         continue;
  249.     }
  250.     if (XParseColor(dpy, cmap, color, &xcolor)) {
  251.         color_table[0][i] = xcolor.red>>8;
  252.         color_table[1][i] = xcolor.green>>8;
  253.         color_table[2][i] = xcolor.blue>>8;
  254.         color_table[3][i] = 0xff;
  255.     } else {
  256.         color_table[0][i] = 0xbe;
  257.         color_table[1][i] = 0xbe;
  258.         color_table[2][i] = 0xbe;
  259.         color_table[3][i] = 0xff;
  260.     }
  261.     }
  262.     /* convert pixmap to RImage */
  263.     p = (int*)xpm.data;
  264.     data = image->data;
  265.     for (i=0; i<xpm.width*xpm.height; i++, p++) {
  266.     *(data++)=color_table[0][*p];
  267.     *(data++)=color_table[1][*p];
  268.     *(data++)=color_table[2][*p];
  269.     *(data++)=color_table[3][*p];
  270.     }
  271.     for(i=0; i<4; i++) {
  272.     free(color_table[i]);
  273.     }
  274.     XpmFreeXpmImage(&xpm);
  275.     return image;
  276. }
  277.  
  278. #endif /* USE_XPM */
  279.